package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.springbootinit.mapper.ClothingHistoryPeriodMapper;
import com.graduation.springbootinit.mapper.ClothingHistoryDetailMapper;
import com.graduation.springbootinit.model.entity.CulturePart.ClothingHistoryPeriod;
import com.graduation.springbootinit.model.entity.CulturePart.ClothingHistoryDetail;
import com.graduation.springbootinit.service.ClothingHistoryService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class ClothingHistoryServiceImpl extends ServiceImpl<ClothingHistoryPeriodMapper, ClothingHistoryPeriod>
        implements ClothingHistoryService {

    @Resource
    private ClothingHistoryDetailMapper detailMapper;

    @Override
    public List<ClothingHistoryPeriod> listPeriods() {
        QueryWrapper<ClothingHistoryPeriod> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort_order");
        return list(queryWrapper);
    }

    @Override
    public List<ClothingHistoryDetail> listDetailsByPeriodId(Long periodId) {
        QueryWrapper<ClothingHistoryDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("period_id", periodId)
                   .orderByAsc("sort_order");
        return detailMapper.selectList(queryWrapper);
    }

    @Override
    public ClothingHistoryDetail getDetailById(Long id) {
        return detailMapper.selectById(id);
    }

    @Override
    public Long addPeriod(ClothingHistoryPeriod period) {
        save(period);
        return period.getId();
    }

    @Override
    public Long addDetail(ClothingHistoryDetail detail) {
        detailMapper.insert(detail);
        return detail.getId();
    }

    @Override
    public void updatePeriod(ClothingHistoryPeriod period) {
        updateById(period);
    }

    @Override
    public void updateDetail(ClothingHistoryDetail detail) {
        detailMapper.updateById(detail);
    }
} 